package cn.cmcc.online.smsapi.ui.model;

import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.cmcc.online.smsapi.ui.util.DataUtil;

public class TransportSelectInfo {
    public static final int TRANSPORT_TRAIN = 1;
    public static final int TRANSPORT_FLIGHT = 2;
    private static final int TRANSPORT_TRAIN_STATION = 3;

    private String title;
    private List<TransportInfo> infoList;

    public static class TransportInfo {
        private String subject;
        private String description;

        public TransportInfo(JSONObject obj, int type) {

            if (type == TRANSPORT_TRAIN) {
                subject = obj.optString("train_num");
            } else if (type == TRANSPORT_FLIGHT) {
                String depart_place = obj.optString("depart_place");
                String arrive_place = obj.optString("arrive_place");
                String depart_airport = obj.optString("depart_airport");
                String arrive_airport = obj.optString("arrive_airport");

                String depart = spliceStr(depart_place, depart_airport);
                String arrive = spliceStr(arrive_place, arrive_airport);
                subject = obj.optString("flight_num");

                if (TextUtils.isEmpty(arrive)) {
                    description = spliceStr(depart, "起飞");
                } else if (TextUtils.isEmpty(depart)) {
                    description = spliceStr("飞往", arrive);
                } else {
                    description = depart + "-" + arrive;
                }
            } else if (type == TRANSPORT_TRAIN_STATION) {
                subject = obj.optString("station_name");
            }
        }

        public String getSubject() {
            return subject;
        }

        public String getDescription() {
            return description;
        }

        private String spliceStr(String str1, String str2) {
            StringBuilder info = new StringBuilder("");
            if (!TextUtils.isEmpty(str1)) {
                info.append(str1).append(" ");
            }
            if (!TextUtils.isEmpty(str2)) {
                info.append(str2);
            }
            return info.toString().trim();
        }
    }

    public TransportSelectInfo(JSONObject obj, int type) {
        if (type == TRANSPORT_TRAIN) {
            title = "选择车次";
        } else if (type == TRANSPORT_FLIGHT) {
            title = "选择航班";
        } else {
            title = "选择";
        }

        infoList = new ArrayList<>();
        try {
            JSONArray dataArray = DataUtil.getMainData(obj);
            for (int i = 0; i < dataArray.length(); i++) {
                JSONObject trainObj = dataArray.optJSONObject(i);
                if (trainObj != null) {
                    infoList.add(new TransportInfo(trainObj, type));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getTitle() {
        return title;
    }

    public List<TransportInfo> getInfoList() {
        return infoList;
    }
}
